package sengine;

import game.Equipment;

public class Character extends Sprite {

    private int attack = 0;
    private int defense = 0;
    private Level level;
    private boolean inAir = false;
    private int health = 100;
    private Equipment equipped;
    private String name;
    private byte direction = 1;

    public Character(int x, int y, int width, int height) {
        super(x, y, width, height);
        setPhysType(Object2D.PhysicsType.MOVABLE);
    }

    public Level getLevel() {
        return level;
    }

    public void setLevel(Level level) {
        this.level = level;
    }

    public void jump() {
        setVy(-375);
        setAy(1980);
        setY(getY() - 4);
    }

    public void moveLeft() {
        setVx(-195);
        direction = -1;
    }

    public void moveRight() {
        setVx(195);
        direction = 1;
    }

    public void stopMove() {
        setVx(0);
    }

    public void stopVertical() {
        setVy(0);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean airborne() {
        return inAir;
    }

    public void inAir() {
        inAir = true;
    }

    public void onGround() {
        inAir = false;
    }

    public void die() {
        rmSelf();
    }

    public int getHealth() {
        return health;
    }

    public void changeHealth(int delta) {
        this.health += delta;
        checkHealth();
    }

    protected void setHealth(int health) {
        this.health = health;
        checkHealth();
    }

    protected void checkHealth() {
        if (health <= 0) {
            die();
        }
    }
    
    public byte getDirection() {
        return direction;
    }

    @Override
    public void update(long millis) {
        if (getY() > 1080 || getY() < 0) {
            die();
        }
        if (equipped != null) {
            equipped.playerLocUpdate(getX(), getY(), getR(), direction);
        }
        super.update(millis);
    }

    public void equip(Equipment tg) {
        equipped = tg;
    }

    public Equipment getEquipment() {
        return equipped;
    }
}
